package com.sfang.common.extension;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CustomApplicationIntializer {
    private static Logger LOGGER = LoggerFactory.getLogger(CustomApplicationIntializer.class);

    public static void beforeApplicationRun() {
        System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "50");
        System.setProperty("rocketmq.client.logLevel","ERROR");

        String env = System.getProperty("env");
        String nacosServerAddr = System.getProperty("nacos.server-addr");
        String nacosNamespace = System.getProperty("nacos.namespace");
        String nacosUserName = System.getProperty("nacos.username");
        String nacosPassword = System.getProperty("nacos.password");
        String serverIP = System.getProperty("server.ip");

        LOGGER.info(String.format(" env:%s nacosServerAddr:%s nacosNamespace:%s nacosUserName:%s nacosPassword:%s serverIP:%s",
                env,
                nacosServerAddr,
                nacosNamespace,
                nacosUserName,
                nacosPassword,
                serverIP
        ));

        if (env != null)
            System.setProperty("spring.cloud.config.profile", env);

        if (nacosServerAddr != null) {
            System.setProperty("spring.cloud.nacos.config.server-addr", nacosServerAddr);
            System.setProperty("spring.cloud.nacos.discovery.server-addr", nacosServerAddr);
        }


        if (nacosNamespace != null) {
            System.setProperty("spring.cloud.nacos.config.namespace", nacosNamespace);
            System.setProperty("spring.cloud.nacos.discovery.namespace", nacosNamespace);
        }

        if (nacosUserName != null) {
            System.setProperty("spring.cloud.nacos.config.username", nacosUserName);
            System.setProperty("spring.cloud.nacos.discovery.username", nacosUserName);
        }

        if (nacosPassword != null) {
            System.setProperty("spring.cloud.nacos.config.password", nacosUserName);
            System.setProperty("spring.cloud.nacos.discovery.password", nacosUserName);
        }

        if(serverIP!=null){
            System.setProperty("spring.cloud.sentinel.transport.client-ip", serverIP);
        }
    }
}
